package com.huawei.esdk.uc.device.u19.callback;

import org.apache.log4j.Logger;

import com.huawei.esdk.platform.callback.ISouthCallbackService;
import com.huawei.esdk.platform.common.bean.callback.CallbackMessage;
import com.huawei.esdk.platform.common.utils.JAXBUtils;
import com.huawei.esdk.platform.common.utils.StringUtils;
import com.huawei.esdk.uc.device.u19.callback.bean.CTCConfState;
import com.huawei.esdk.uc.device.u19.callback.bean.CTCUserState;
import com.huawei.esdk.uc.domain.model.callback.CeeStatus;
import com.huawei.esdk.uc.domain.model.callback.ConfStatus;
import com.huawei.esdk.uc.domain.util.u19.U19Utils;
import com.huawei.esdk.uc.southcommu.callback.CallbackSouthBase;

public class U19CallbackService extends CallbackSouthBase implements ISouthCallbackService
{
    private static final Logger LOGGER = Logger.getLogger(U19CallbackService.class);
    
    @Override
    public String processCallbackMessage(String inMessage)
    {
        String res;
        if (StringUtils.isEmpty(inMessage))
        {
            res = "The incoming callback message is empty";
            LOGGER.warn(res);
            return res;
        }
        
        if (inMessage.contains("notify_ctcuserstate"))
        {
            res = processUserStateMsg(inMessage);
        }
        else if (inMessage.contains("notify_ctcconferstate"))
        {
            res = processConferStateMsg(inMessage);
        }
        else
        {
            res = "The incoming callback message cannot be recognized by eSDK.";
        }
        
        return res;
    }
    
    private String processUserStateMsg(String inMsg)
    {
        CTCUserState userState = (CTCUserState)JAXBUtils.xml2Bean(CTCUserState.class, inMsg);
        CeeStatus payload = new CeeStatus();
        payload.setConfId(userState.getBean().getConfId());
        payload.setCee(userState.getBean().getCee());
        payload.setStatus(userState.getBean().getStatus());
        
        CallbackMessage callbackMessage = buildCallbackMessage(payload, null, "ctcuserstate", "ctc");
        callbackMessage.getCallbackItfInfo().setDevId(U19Utils.getU19DeviceId());
        callbackMessage.getCallbackItfInfo().setConnectionId("_");
        notifyCollector.publishNotify(callbackMessage);
        return "<notify-ctcuserstate-res></notify-ctcuserstate-res>";
    }
    
    private String processConferStateMsg(String inMsg)
    {
        CTCConfState confState = (CTCConfState)JAXBUtils.xml2Bean(CTCConfState.class, inMsg);
        ConfStatus payload = new ConfStatus();
        payload.setConfId(confState.getBean().getConfId());
        payload.setStatus(confState.getBean().getStatus());
        
        CallbackMessage callbackMessage = buildCallbackMessage(payload, null, "ctcconferstate", "ctc");
        callbackMessage.getCallbackItfInfo().setDevId(U19Utils.getU19DeviceId());
        callbackMessage.getCallbackItfInfo().setConnectionId("_");
        notifyCollector.publishNotify(callbackMessage);
        return "<notify-ctcconferstate-res></notify-ctcconferstate-res>";
    }
    
}
